
package com.pharmacy.InventoryManage.controller;

import com.pharmacy.InventoryManage.entity.Inventory;
import com.pharmacy.InventoryManage.service.InventoryService;
import com.pharmacy.UserManage.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/inventory")
public class InventoryController {

    @Autowired
    private InventoryService inventoryService;

    // 获取所有库存信息
    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getAllInventories(HttpSession session) {
        Map<String, Object> response = new HashMap<>();
        User currentUser = (User) session.getAttribute("currentUser");

        if (currentUser == null) {
            response.put("success", false);
            response.put("message", "用户未登录");
            return ResponseEntity.status(401).body(response);
        }

        try {
            List<Inventory> inventories = inventoryService.getAllInventories();
            response.put("success", true);
            response.put("data", inventories);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取库存列表失败");
            return ResponseEntity.status(500).body(response);
        }
    }

    // 获取低库存预警
    @GetMapping("/low-stock")
    public ResponseEntity<Map<String, Object>> getLowStockInventories(HttpSession session) {
        Map<String, Object> response = new HashMap<>();
        User currentUser = (User) session.getAttribute("currentUser");

        if (currentUser == null) {
            response.put("success", false);
            response.put("message", "用户未登录");
            return ResponseEntity.status(401).body(response);
        }

        try {
            List<Inventory> lowStockInventories = inventoryService.getLowStockInventories();
            response.put("success", true);
            response.put("data", lowStockInventories);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "获取低库存信息失败");
            return ResponseEntity.status(500).body(response);
        }
    }

    // 更新库存（需要库存管理员权限）
    @PutMapping("/update")
    public ResponseEntity<Map<String, Object>> updateInventory(@RequestBody Inventory inventory, HttpSession session) {
        Map<String, Object> response = new HashMap<>();
        User currentUser = (User) session.getAttribute("currentUser");

        if (currentUser == null || !"inventory".equals(currentUser.getRole())) {
            response.put("success", false);
            response.put("message", "权限不足");
            return ResponseEntity.status(403).body(response);
        }

        if (inventoryService.updateInventory(inventory)) {
            response.put("success", true);
            response.put("message", "库存更新成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("success", false);
            response.put("message", "库存更新失败");
            return ResponseEntity.badRequest().body(response);
        }
    }

    // 检查库存是否充足
    @GetMapping("/check-stock")
    public ResponseEntity<Map<String, Object>> checkStock(@RequestParam Integer medicineId,
                                                          @RequestParam Integer quantity,
                                                          HttpSession session) {
        Map<String, Object> response = new HashMap<>();
        User currentUser = (User) session.getAttribute("currentUser");

        if (currentUser == null) {
            response.put("success", false);
            response.put("message", "用户未登录");
            return ResponseEntity.status(401).body(response);
        }

        boolean hasEnoughStock = inventoryService.checkStock(medicineId, quantity);
        response.put("success", true);
        response.put("hasEnoughStock", hasEnoughStock);
        return ResponseEntity.ok(response);
    }
}